package com.tyros.common.config.mybatis;

import com.tyros.common.datasource.lock.DatabaseLock;
import com.tyros.common.utils.Lock;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author zhangshuaiyin
 * @date 2022/3/24 16:41
 */
@Configuration
public class MybatisConfig {
    
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(value = "lock.db.enabled", havingValue = "true")
    public Lock databaseLock(DataSource dataSource, @Value("${lock.db.sql:}") String lockSql) {
        return new DatabaseLock(dataSource, lockSql);
    }
}
